﻿using System.Text.RegularExpressions;

namespace SearchInFiles.Lib.Search
{
    public class ExpressionSearcher : SearcherAbstract
    {
        private Regex regex;
        public ExpressionSearcher(string searchParameters, int FrontLength, int AfterLength) : base(FrontLength, AfterLength){
            regex = new Regex(searchParameters,RegexOptions.IgnoreCase|RegexOptions.Compiled);
        }

        public override bool Search(SearchRequest Request)
        {
            bool Result = false;
            if (string.IsNullOrWhiteSpace(Request.Value))
                return false;
            foreach(Match match in regex.Matches(Request.Value))
            {
                if (!match.Success)
                    continue;
                
                Results.Add(new SearchResult(match.Index, match.Value, SearchResult.GetPartialContext(Request.Value, match.Value, match.Index, FrontLength, AfterLength), Request.RequestElement, Request.SourceInfo));
                if (!Result) Result = true;
            }
            return Result;
        }
    }
}
